package com.dq.controller;

import com.dq.mapper.CommentMapper;
import com.dq.mapper.CommentReplyMapper;
import com.dq.pojo.Comment;
import com.dq.pojo.CommentReply;
import com.dq.wordfilter.WordContext;
import com.dq.wordfilter.WordFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
public class CommentController {
    @Autowired
    CommentMapper commentMapper;

    @Autowired
    CommentReplyMapper commentReplyMapper;

    @Autowired
    WordContext wordContext;



    @GetMapping("/addComment")
    public String addComment(@RequestParam(value = "userId",required = false)String userId,
                             @RequestParam(value = "blogId",required = false)String blogId,
                             @RequestParam(value = "content",required = false)String content){
        WordFilter filter = new WordFilter(wordContext);
        String newContent = filter.replace(content, 2,'*');

        String uuid= UUID.randomUUID().toString().replaceAll("-", "");
        commentMapper.addOneComment(new Comment(uuid,blogId,userId,newContent,new Date()));

        return "redirect:/goods/good/"+blogId;
    }

    @PostMapping("/addCommentReply")
    public String addCommentReply(@RequestParam(value = "blogId",required = false)String blogId,
            @RequestParam(value = "commentId",required = false)String commentId,
                                @RequestParam(value = "userId",required = false)String userId,
                                @RequestParam(value = "replyUserId",required = false)String replyUserId,
                                @RequestParam(value = "content",required = false)String content){
        WordFilter filter = new WordFilter(wordContext);
        String newContent = filter.replace(content, 1,'*');

        String uuid= UUID.randomUUID().toString().replaceAll("-", "");
        commentReplyMapper.addReply(new CommentReply(uuid,commentId,userId,replyUserId,newContent,new Date()));
        return "redirect:/goods/good/"+blogId;
    }

}
